
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
from service import logger

class Mail:

    def __init__(self, address, port, account, password, style):
        self.address = address
        self.port = port
        self.account = account
        self.password = password
        self.style = style

    def send_emails(self):
        message = MIMEMultipart()
        message["Accept-Language"] = "zh-CN"
        message['From'] = Header("小测试", 'utf-8')
        message['To'] = Header("巨佬们请查收", 'utf-8')
        message['Subject'] = Header('测试报告', 'utf-8')

        if self.style == "html":
            with open('./report/test_report.html', 'rb') as html_file:
                message.attach(MIMEText(bytes.decode(html_file.read()), self.style, 'utf-8'))
        else:
            message.attach(MIMEText('测试完成，发送测试报告', self.style, 'utf-8'))

        with open('./documents/upload_full_stack_test.xlsx', 'rb') as annex_file:
            att = MIMEText(annex_file.read(), 'base64', 'utf-8')
            att["Content-Type"] = 'application/octet-stream'
            att["Content-Disposition"] = 'attachment; filename="upload_full_stack_test.xlsx"'
            message.attach(att)

        try:
            smtp = smtplib.SMTP_SSL(self.address, self.port)
            smtp.login(self.account, self.password)
            smtp.sendmail(self.account, ["2677059047@qq.com", "18570683431@163.com"], message.as_string())
            logger.debug("邮件发送成功")

        except smtplib.SMTPException as sse:
            logger.debug(repr(sse))























































































